package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.UpdateServiceEvent;
	import net.optix.ip.model.ModelLocator;
	import net.optix.ip.vo.ClientVO;
	import net.optix.ip.vo.FirmVO;
	
	import views.ViewInstance;

	public class UpdateServiceCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();

		private var role:int;
		private var userID:int;
		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:UpdateServiceEvent = UpdateServiceEvent( event );
			
			//add 2009-3-30
			this.role	=	myEvent.vo.role;
			this.userID	=	myEvent.vo.userID;
			
			var delegate:ManagerDelegate = new ManagerDelegate(  );     
			delegate.updateService(this, myEvent.vo );
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{		
			trace(event.result);
			var title:String	=	"Result of update service ";
			var message:String;
			
			try
			{
				var	result:int				=	event.result.result;
				switch(result)
				{
					case -1:
						message	=	"The invention can't be editing now , may it is already be published !";
						break;
					case 0:
						message	=	"Program error!";
						break;
					case 1:
						message	=	"Successfully update service!";
						updateService();
						break;
				}
			}
			catch(error:Error)
			{
				message	=	"Program error";
			}
			
			model.bSubmit	=	true;
			
			
			ViewInstance.showMessage(title,message);
			
		}
		
		public function updateService():void
		{
			try
			{
				switch(this.role)
				{
					case 1:
						for(var i:int=0; i<model.clients.length; i++)
						{
							var client:ClientVO	=	model.clients[i];
							if(client.userID	==	this.userID)
							{
								client.bService	=	(client.bService+1)%2;
								model.clients[i]=	client;
							}
						}
						break;
					case 3:
						for(var i:int=0; i<model.firms.length; i++)
						{
							var firm:FirmVO	=	model.firms[i];
							if(firm.userID	==	this.userID)
							{
								firm.bService	=	(firm.bService+1)%2;
								model.firms[i]=	firm;
							}
						}
						break;
				}
			}
			catch(error:Error)
			{
				trace(error.getStackTrace());
			}
		}
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			ViewInstance.showMessage("Result of update service ","Program error");
		}

	}
}